package com.ssyc.gsk_teacher_appraisal.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseItemDraggableAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ssyc.common.utils.DateUtils;
import com.ssyc.common.utils.SPUtil;
import com.ssyc.common.utils.UIHelper;
import com.ssyc.gsk_teacher_appraisal.R;
import com.ssyc.gsk_teacher_appraisal.activity.TkTeacherAppraisalQuestionActivity;
import com.ssyc.gsk_teacher_appraisal.bean.StudentTestInfo;
import com.ssyc.gsk_teacher_appraisal.constant.TeacherAppKeys;

import java.util.List;

/**
 * 学生端首页考试列表的适配器
 * Created by Kevin on 2018/10/24.
 */

public class StudentMainTestRvAdapter extends BaseItemDraggableAdapter<StudentTestInfo.DataBean, BaseViewHolder> {
    private Context context;

    public StudentMainTestRvAdapter(Context context, int layoutResId, List<StudentTestInfo.DataBean> data) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, StudentTestInfo.DataBean item) {

        //设置考试标题
        helper.setText(R.id.tv_name, item.getRange() + item.getTag());

        //设置考试时间
        helper.setText(R.id.tv_detail_time, item.getCreate_time());

        //判断是否完成考试
        if (item.getIs_done().equals("1")) {
            //已完成
            helper.getView(R.id.iv_set_state).setVisibility(View.VISIBLE);
            helper.getView(R.id.ll_details).setBackgroundColor(context.getResources().getColor(R.color.text_rv_bg));
            helper.setImageDrawable(R.id.iv_set_state, context.getDrawable(R.drawable.teacher_test_finish));
            helper.getView(R.id.tv_last_time).setVisibility(View.GONE);
        } else if (item.getIs_done().equals("0")) {
            //可答题
            helper.getView(R.id.iv_set_state).setVisibility(View.VISIBLE);
            helper.getView(R.id.ll_details).setBackgroundColor(context.getResources().getColor(R.color.text_rv_bg));
            helper.setImageDrawable(R.id.iv_set_state, context.getDrawable(R.drawable.teacher_test_nofinish));
            //截止日期
            helper.getView(R.id.tv_last_time).setVisibility(View.VISIBLE);

            Log.i("test", item.getExpiration_time());

            helper.setText(R.id.tv_last_time, "离试卷失效还剩："
                    + DateUtils.lastDate(item.getExpiration_time()));

        } else if (item.getIs_done().equals("2")) {
            //已过期
            helper.getView(R.id.iv_set_state).setVisibility(View.GONE);
            helper.getView(R.id.ll_details).setBackgroundColor(context.getResources().getColor(R.color.cp_overdue_bg));
            helper.getView(R.id.tv_last_time).setVisibility(View.GONE);
        }


        //查看详情的布局
        LinearLayout ll = helper.getView(R.id.ll_details);
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //  3老师查看某个学生的成绩和学生查看自己的成绩(34号接口)  4学生答题(32号接口)
                if (item.getIs_done().equals("0")) {
                    SPUtil.put(context, TeacherAppKeys.AUTHERTYPE, 4);
                } else if (item.getIs_done().equals("1")) {
                    SPUtil.put(context, TeacherAppKeys.AUTHERTYPE, 3);
                } else if (item.getIs_done().equals("2")) {
                    //已过期
                    UIHelper.showToast(context, "测评卷已失效，请联系老师再次授权");
                    return;
                } else {
                    //已过期
                    UIHelper.showToast(context, "测评卷已失效，请联系老师再次授权");
                    return;
                }
                //保存条目的试卷名称
                SPUtil.put(context,"shareconent", item.getRange()+item.getTag());
                //跳转到查看详情的界面
                Intent intent = new Intent(context, TkTeacherAppraisalQuestionActivity.class);
                intent.putExtra("rowId", item.getROW_ID2());
                intent.putExtra("commitrowid", item.getROW_ID());
                intent.putExtra("h5rowid", item.getROW_ID());
                intent.putExtra("exam_id", item.getExam_id());
                intent.putExtra("school_id", item.getSchool_id());
                intent.putExtra("class_num", item.getClass_num());
                intent.putExtra("teacher", item.getTeacher());
                intent.putExtra("time", Integer.parseInt(item.getTime()) * 60 * 1000);
                intent.putExtra("name", item.getName());
                intent.putExtra("testName", item.getTag());
                intent.putExtra("rangetag", item.getRange() + item.getTag());
                context.startActivity(intent);
            }
        });
    }
}
